package com.twu.biblioteca;

import com.sun.org.apache.bcel.internal.generic.ACONST_NULL;

import javax.swing.text.MaskFormatter;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: luisarmando
 * To change this template use File | Settings | File Templates.
 */
public class User {
    private static int login_generator = 1111111;
    private int login;
    private String password;
    private String name;
    private String email;
    private String phone;

    public User(String password, String name, String email, String phone) {
        this.login = User.login_generator++;
        this.password = password;
        this.name = name;
        this.email = email;
        this.phone = phone;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin(){
        MaskFormatter ft = null;
        try {
            ft = new MaskFormatter("###-####");
            ft.setValueContainsLiteralCharacters(false);
            return ft.valueToString(Integer.toString(login));
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return "";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public static User authenticate(int login, String password){
        List<User> users = User.getUsers();
        for (User user : users){
            if (user.login == login && user.password.equals(password)){
                return user;
            }
        }
        return null;
    }

    public static User authenticate(String login, String password){
        MaskFormatter ft = null;
        try {
            ft = new MaskFormatter("###-####");
            ft.setValueContainsLiteralCharacters(false);
            return authenticate(Integer.parseInt(ft.stringToValue(login).toString()), password);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;

    }

    @Override
    public String toString() {
        return getLogin()+"\t"+getName()+"\t"+getEmail()+'\t'+getPhone();
    }

    private static List<User> users;

    public static List<User> getUsers(){
        if (User.users == null || User.users.isEmpty()){
            users = new ArrayList<User>();
            users.add(new User("123", "user1", "mail1@mail.com", "321"));
            users.add(new User("foo", "user2", "mail2@mail.com", "321"));
            users.add(new User("bla", "user3", "mail3@mail.com", "321"));
            users.add(new User("demo", "user4", "mail4@mail.com", "321"));
            users.add(new User("user5", "user5", "mail5@mail.com", "321"));
        }
        return users;
    }
}
